package cn.zl.springbootredis.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: zhanglang
 * @Date: 2020/9/7 16:33
 */
//用来获取Springboot创建好的工程
@Component
public class ApplicationContextUtils implements ApplicationContextAware {
    //保留下来的工厂
    private static ApplicationContext applicationContext;
    //将创建好工厂以参数形式传递给这个类
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //提供工厂中获取对象的方法
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
}
